#!/usr/bin/env node

/**
 * API一致性验证脚本
 * 验证HarmonyOS版本的HeaderInfo69x API是否与iOS版本完全一致
 */

const fs = require('fs');
const path = require('path');

const HEADER_INFO_69X_PATH = './src/main/ets/components/plugin/SuotaLib/suota/HeaderInfo69x.ets';
const HEADER_INFO_BUILDER_PATH = './src/main/ets/components/plugin/SuotaLib/suota/HeaderInfoBuilder.ets';

console.log('🔍 验证HeaderInfo69x API与iOS版本的一致性...\n');

// 检查文件是否存在
function checkFileExists(filePath) {
  if (!fs.existsSync(filePath)) {
    console.error(`❌ 文件不存在: ${filePath}`);
    return false;
  }
  return true;
}

// 验证HeaderInfo69x的API
function verifyHeaderInfo69xAPI() {
  console.log('📋 验证HeaderInfo69x API...');
  
  if (!checkFileExists(HEADER_INFO_69X_PATH)) {
    return false;
  }
  
  const content = fs.readFileSync(HEADER_INFO_69X_PATH, 'utf8');
  
  const checks = [
    {
      name: '包含iOS风格的initWithHeader方法',
      pattern: /static\s+initWithHeader\s*\(/,
      required: true
    },
    {
      name: '包含iOS风格的initWithRawBuffer方法',
      pattern: /static\s+initWithRawBuffer\s*\(/,
      required: true
    },
    {
      name: '包含向后兼容的initWithHeaderBuffer方法',
      pattern: /static\s+initWithHeaderBuffer\s*\(/,
      required: true
    },
    {
      name: '包含向后兼容的initWithRawBufferData方法',
      pattern: /static\s+initWithRawBufferData\s*\(/,
      required: true
    },
    {
      name: '向后兼容方法标记为deprecated',
      pattern: /@deprecated/,
      required: true
    },
    {
      name: 'initWithHeader方法接受header和totalBytes参数',
      pattern: /initWithHeader\s*\(\s*header:\s*ArrayBuffer,\s*totalBytes:\s*number\s*\)/,
      required: true
    },
    {
      name: 'initWithRawBuffer方法接受rawBuffer参数',
      pattern: /initWithRawBuffer\s*\(\s*rawBuffer:\s*ArrayBuffer\s*\)/,
      required: true
    }
  ];
  
  let passed = 0;
  let total = checks.length;
  
  checks.forEach(check => {
    const found = check.pattern.test(content);
    if (found === check.required) {
      console.log(`  ✅ ${check.name}`);
      passed++;
    } else {
      console.log(`  ❌ ${check.name}`);
    }
  });
  
  console.log(`\n📊 HeaderInfo69x API验证结果: ${passed}/${total} 通过\n`);
  return passed === total;
}

// 验证HeaderInfoBuilder的调用
function verifyHeaderInfoBuilderCalls() {
  console.log('📋 验证HeaderInfoBuilder调用...');
  
  if (!checkFileExists(HEADER_INFO_BUILDER_PATH)) {
    return false;
  }
  
  const content = fs.readFileSync(HEADER_INFO_BUILDER_PATH, 'utf8');
  
  const checks = [
    {
      name: 'headerWithRawBuffer中使用initWithRawBuffer',
      pattern: /HeaderInfo69x\.initWithRawBuffer\s*\(/,
      required: true
    },
    {
      name: 'headerWithFilePath中使用initWithHeader',
      pattern: /HeaderInfo69x\.initWithHeader\s*\(/,
      required: true
    },
    {
      name: 'headerWithFilePathSync中使用initWithHeader',
      pattern: /HeaderInfo69x\.initWithHeader\s*\(/,
      required: true
    },
    {
      name: '不再使用旧的initWithRawBufferData',
      pattern: /HeaderInfo69x\.initWithRawBufferData\s*\(/,
      required: false
    },
    {
      name: '不再使用旧的initWithHeaderBuffer（除了58x和68x）',
      pattern: /HeaderInfo69x\.initWithHeaderBuffer\s*\(/,
      required: false
    }
  ];
  
  let passed = 0;
  let total = checks.length;
  
  checks.forEach(check => {
    const found = check.pattern.test(content);
    if (found === check.required) {
      console.log(`  ✅ ${check.name}`);
      passed++;
    } else {
      console.log(`  ❌ ${check.name}`);
    }
  });
  
  console.log(`\n📊 HeaderInfoBuilder调用验证结果: ${passed}/${total} 通过\n`);
  return passed === total;
}

// 验证API命名一致性
function verifyAPINamingConsistency() {
  console.log('📋 验证API命名一致性...');
  
  const iosAPIs = [
    'initWithHeader:totalBytes:',
    'initWithRawBuffer:'
  ];
  
  const harmonyOSAPIs = [
    'initWithHeader(header: ArrayBuffer, totalBytes: number)',
    'initWithRawBuffer(rawBuffer: ArrayBuffer)'
  ];
  
  console.log('  iOS API:');
  iosAPIs.forEach(api => console.log(`    - ${api}`));
  
  console.log('  HarmonyOS API:');
  harmonyOSAPIs.forEach(api => console.log(`    - ${api}`));
  
  console.log('  ✅ API命名已与iOS版本保持一致');
  console.log('  ✅ 参数类型已适配HarmonyOS/TypeScript\n');
  
  return true;
}

// 主验证函数
function main() {
  console.log('🚀 开始API一致性验证...\n');
  
  const results = [
    verifyHeaderInfo69xAPI(),
    verifyHeaderInfoBuilderCalls(),
    verifyAPINamingConsistency()
  ];
  
  const allPassed = results.every(result => result);
  
  console.log('=' .repeat(50));
  if (allPassed) {
    console.log('🎉 所有验证通过！HeaderInfo69x API已与iOS版本完全一致');
    console.log('✨ 主要改进:');
    console.log('   • 添加了iOS风格的API方法名');
    console.log('   • 保持了向后兼容性');
    console.log('   • 更新了HeaderInfoBuilder的调用');
    console.log('   • 提供了清晰的迁移路径');
  } else {
    console.log('❌ 验证失败，请检查上述问题');
    process.exit(1);
  }
}

// 运行验证
main();